package leetCode;

import java.util.ArrayList;
import java.util.List;

/**
 * 回溯算法
 *
 * @author 司松坡
 * @createTime 2023年03月19日
 */
public class LeetCode46 {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        boolean[] isTrue = new boolean[nums.length];
        //调用回溯算法
        recall(res,new ArrayList<>(),isTrue,nums);
        return res;
    }

    public void recall(List<List<Integer>> res, List<Integer> instantly, boolean[] isTrue, int[] nums) {
        if (instantly.size() == nums.length) {
            res.add(new ArrayList<>(instantly));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            //做选择
            if (!isTrue[i]){
                isTrue[i] = true;
                instantly.add(nums[i]);
                recall(res,instantly,isTrue,nums);
                isTrue[i] = false;
                instantly.remove(instantly.size()-1);
            }
        }
    }
}
